//
//  OMGoodDetailHeadView.m
//  OMENGO
//
//  Created by 梁尚嘉 on 2017/4/19.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMGoodDetailHeadView.h"
#import "OMProduct.h"
#import "NSAttributedString+SizeRemind.h"
@interface OMGoodDetailHeadView()
@property (nonatomic, strong) UILabel *brandLabel;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *specLabel;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UILabel *buyLabel;
@property (nonatomic, strong) UIButton *plusButton;

@property (nonatomic, strong) UILabel *stockLabel;

@property (nonatomic, weak) OMProduct *product;
@end

@implementation OMGoodDetailHeadView


- (instancetype)init
{
    self = [super init];
    [self configure];
    return self;
}

- (void)configure
{
    self.frame = CGRectMake(0, 0, SM_SCREEN_WIDTH, SM_SCREEN_WIDTH + SM_SIZE(200));
    self.backgroundColor = SM_whiteColor;

    _header = [STParallaxHeader parallaxHeaderViewWithCGSize:CGSizeMake(SM_SCREEN_WIDTH, SM_SCREEN_WIDTH)];
    _header.backgroundColor = SM_naviShadowColor;
    [self addSubview:_header];
    [_header mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(@(SM_SIZE(0)));
        make.width.equalTo(_header.mas_height);
    }];

    UIImageView *maskView = [UIImageView new];
    maskView.image = SMPNGImage(@"ic_good_detail_gradient");
    maskView.alpha = .3f;
    maskView.contentMode = UIViewContentModeScaleAspectFill;
    [_header addSubview:maskView];
    [maskView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(@0);
    }];

    _brandLabel = [UILabel new];
    _brandLabel.font = SM_CBFont(35);
    _brandLabel.textColor = SM_blackColor;
    _brandLabel.text = @"百威";
    _brandLabel.numberOfLines = 0;
    [self addSubview:_brandLabel];
    [_brandLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(SM_SIZE(30)));
        make.top.equalTo(_header.mas_bottom).offset(SM_SIZE(20));
        make.right.equalTo(@(SM_SIZE(-30)));
    }];

    _nameLabel = [UILabel new];
    _nameLabel.textColor = SM_blackColor;
    _nameLabel.font = SM_CFont(35)  ;

    _specLabel = [UILabel new];
    _specLabel.font = SM_CFont(27);
    _specLabel.textColor = SM_grayColor;
    [self addSubview:_specLabel];
    [_specLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_brandLabel.mas_left);
        make.top.equalTo(_brandLabel.mas_bottom).offset(SM_SIZE(10));
    }];

    _priceLabel = [UILabel new];
    _priceLabel.textColor = SM_dotRedColor;
    _priceLabel.font = SM_NFont(65);
    [self addSubview:_priceLabel];
    [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_brandLabel.mas_left);
        make.top.equalTo(_brandLabel.mas_bottom).offset(SM_SIZE(15));
    }];


//    _plusButton = [UIButton new];
//    [_plusButton setImage:[UIImage imageNamed:@"home_category_plus"] forState:UIControlStateNormal];
//    [self addSubview:_plusButton];
//    @weakify(self);
//    [[_plusButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
//        @strongify(self);
//        [self didSelectBasketButton:x];
//    }];
//    [_plusButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.equalTo(@(SM_SIZE(-30)));
//        make.bottom.equalTo(_priceLabel.mas_bottom);
//    }];

    _stockLabel = [UILabel new];
    _stockLabel.textColor = SM_grayColor;
    _stockLabel.font = SM_CFont(26);
    [self addSubview:_stockLabel];
    [_stockLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(SM_SIZE(-30)));
        make.bottom.equalTo(self.priceLabel.mas_bottom).offset(SM_SIZE(-10));
    }];

    UIView *bottomLine = [UIView new];
    bottomLine.backgroundColor = SM_naviShadowColor;
    [self addSubview:bottomLine];
    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(@0);
        make.height.equalTo(@.5f);
    }];

}

- (void)configureWithObj:(id)obj
{
    _product = obj;
    [self.header.imageView kt_imageWithURLStr:self.product.image placeHolder:[UIImage st_imageNamed:@"homepage-loading750.png" type:nil] withSize:CGSizeZero];

    NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString: _product.brand?:@"" attributes:@{NSFontAttributeName:SM_CBFont(35)}];
    !title.string.length?:[title appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
    [title appendAttributedString:[[NSAttributedString alloc] initWithString:_product.name?:@"" attributes:@{NSFontAttributeName:self.nameLabel.font}]];


    //规格
    if (_product.unit.length) {
        [title  appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n" attributes:nil]];
        [title  appendAttributedString:[[NSAttributedString alloc] initWithString:_product.unit  attributes:@{NSFontAttributeName:_specLabel.font,NSForegroundColorAttributeName:_specLabel.textColor}]];
    }

    //描述
    if (_product.caption.length) {
        [title  appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n" attributes:nil]];
        [title  appendAttributedString:[[NSAttributedString alloc] initWithString:_product.caption  attributes:@{NSFontAttributeName:SM_CFont(28),NSForegroundColorAttributeName:SM_dotRedColor}]];
    }

    NSMutableParagraphStyle *pStyle = [NSMutableParagraphStyle new];
    pStyle.lineSpacing = SM_SIZE(0);
    pStyle.paragraphSpacing = SM_SIZE(10);
    [title addAttributes:@{NSParagraphStyleAttributeName:pStyle} range:NSMakeRange(0, title.string.length)];
    _brandLabel.attributedText = title;

    //价格
    NSString *price = _product.price.floatValue == 0 ?@"线上预约":[[NSString stringWithFormat:@"￥%.2f",_product.price.floatValue]  stringByReplacingOccurrencesOfString:@".00" withString:@""];
    NSMutableAttributedString *attributedPrice = [[NSMutableAttributedString alloc]initWithString:price];
    NSRange rangOfMoneySign = [price rangeOfString:@"￥"];
    [attributedPrice addAttributes:@{NSFontAttributeName:SM_CFont(33)} range:rangOfMoneySign];
    _priceLabel.attributedText = attributedPrice;

    //库存
    NSMutableAttributedString *stock = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"库存：%ld",_product.availableStock]];
    if (_product.availableStock<10) {
        NSRange range = [stock.string rangeOfString:[@(_product.availableStock)stringValue]];
        [stock addAttributes:@{NSForegroundColorAttributeName:SM_dotRedColor} range:range];
    }
    _stockLabel.attributedText = stock;

    CGFloat height = [title boundSize:CGSizeMake(SM_SCREEN_WIDTH - 2*SM_SIZE(30), CGFLOAT_MAX)].height;
    NSLog(@"%.2f",height);
    if (_product.promotions.count) {

        UIView *line = [UIView new];
        line.backgroundColor = SM_naviShadowColor;
        [self addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(SM_SIZE(30)));
            make.right.equalTo(@(SM_SIZE(-30)));
            make.top.equalTo(self.priceLabel.mas_bottom).offset(SM_SIZE(25));
            make.height.equalTo(@(.5f));
        }];

        UIView *containView = [UIView new];
        [self addSubview:containView];
        [containView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(@0);
            make.top.equalTo(line.mas_bottom);
        }];

        OMPromotion *promotion = _product.promotions[0];
        UIButton *tagButton = [UIButton new];
        [tagButton setTitle:promotion.name forState:UIControlStateNormal];
        [tagButton setTitleColor:SM_thumeColor forState:UIControlStateNormal];
        [tagButton.titleLabel setFont:SM_CFont(23)];
        tagButton.layer.cornerRadius = SM_SIZE(3);
        tagButton.clipsToBounds = YES;
        tagButton.layer.borderColor = SM_thumeColor.CGColor;
        tagButton.layer.borderWidth = .5f;
        tagButton.contentEdgeInsets = UIEdgeInsetsMake(SM_SIZE(2), SM_SIZE(5), SM_SIZE(2), SM_SIZE(5));
        [containView addSubview:tagButton];
        [tagButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.priceLabel.mas_left);
            make.centerY.equalTo(containView.mas_centerY);
        }];

        UILabel *tagLabel = [UILabel new];
        tagLabel.text = promotion.title;
        tagLabel.font = SM_CFont(25);
        tagLabel.textColor = SM_darkGrayColor;
        [containView addSubview:tagLabel];
        [tagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(tagButton.mas_right).offset(SM_SIZE(15));
            make.right.equalTo(@(SM_SIZE(-30)));
            make.centerY.equalTo(tagButton.mas_centerY);
        }];

        height += SM_SIZE(20) + SM_SIZE(30) + SM_SIZE(25);
    }

    self.frame = CGRectMake(0, 0, SM_SCREEN_WIDTH, SM_SCREEN_WIDTH + SM_SIZE(140) + height);

}

@end
